package NC;

import java.util.ArrayList;

public class NC8 {
    private static ArrayList<ArrayList<Integer>> ans = new ArrayList<>();

    public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
        // write code here
        dfs(root, sum, new ArrayList<>());
        return ans;
    }

    public static void dfs(TreeNode root, int sum, ArrayList<Integer> path) {
        if (root == null) return;
        path.add(root.val);
        sum -= root.val;
        if (root.left == null && root.right == null && sum == 0) {
            ans.add(new ArrayList<>(path)); // 需要深拷贝
        }
        dfs(root.left, sum, path);
        dfs(root.right, sum, path);
        path.remove(path.size() - 1);
    }
}
